
module two {
	export class Box extends eui.Component implements eui.UIComponent {
		private view: BaseComponent;
		private rect: eui.Rect;
		public constructor(backClick: boolean) {
			super();

			let rect = this.rect = new eui.Rect();
			this.addChild(rect);
			rect.fillColor = 0x000000;
			rect.fillAlpha = 0.5;
			rect.left = rect.right = rect.top = rect.bottom = 0;

			this.percentHeight = 100;
			this.percentWidth = 100;

			if (backClick) {
				rect.once(egret.TouchEvent.TOUCH_TAP, () => {
					two.closeBox(this.view);
				}, this);
			}
		}

		hasRect(): boolean {
			return this._hasRect;
		}

		setRectHide(isHide: boolean) {
			if (isHide) {
				this.rect.visible = false;
			}
			else {
				this.rect.visible = true;
			}
		}

		private viewHashID: number;
		isSame(view: BaseComponent): boolean {
			return this.viewHashID == view.hashCode;
		}

		private _hasRect: boolean;
		addView(view: BaseComponent, fillAlpha: number, imede: boolean): void {
			this._hasRect = fillAlpha != -1;
			if (fillAlpha == -1) {
				this.rect.visible = false;
			}
			else {
				this.rect.fillAlpha = fillAlpha;
				this.rect.visible = true;
			}

			view.isClosed = false;
			view.percentHeight = 100;
			view.percentWidth = 100;

			this.viewHashID = view.hashCode;
			this.view = view;

			this.visible = true;
			this.addChild(view);

			if (imede) {

			}
			else {
				this.view.scaleX = this.view.scaleY = 0;
				this.view.alpha = 0;
				egret.Tween.get(this.view).to({ scaleX: 1, scaleY: 1, alpha: 1 }, 400, egret.Ease.backOut).call(() => {
				}, this);
			}
			// */
		}

		public isBoxClosed(): boolean {
			return this.view != null ? this.view.isClosed : true;
		}

		removeView(imede: boolean): void {
			if (imede) {
				this.view.isClosed = true;
				this.removeChild(this.view);
				this.visible = false;
				this.view = null;
				this.parent.removeChild(this);

				showTopMask();
			}
			else {
				let t = this.view.touchEnabled;
				let c = this.view.touchChildren;
				this.view.touchEnabled = false;
				this.view.touchChildren = false;
				this.view.isClosed = true;
				egret.Tween.get(this.view).to({ scaleX: 0, scaleY: 0, alpha: 0 }, 400, egret.Ease.backIn).call(() => {
					this.removeChild(this.view);

					this.view.touchEnabled = t;
					this.view.touchChildren = c;

					this.visible = false;
					this.view = null;
					this.parent.removeChild(this);

					showTopMask();
				}, this);
			}
		}
	}

	function showTopMask(): void {
		for (let i: number = 0; i < boxLayer.numChildren - 1; i++) {
			let box: Box = boxLayer.getChildAt(i) as Box;
			if (box.isSame) {
				box.setRectHide(true);
			}
		}

		for (let i = boxLayer.numChildren - 1; i >= 0; i--) {
			let box: Box = boxLayer.getChildAt(i) as Box;
			if (box.isSame && box.hasRect()) {
				box.setRectHide(false);
				return;
			}
		}
	}

	/**
	 * 显示弹窗
	 * @view 弹窗内容，除去遮罩外
	 * @fillAlpha 遮罩透明度 0-1 有遮罩， -1 无遮罩
	 */
	export function showBox(view: BaseComponent, fillAlpha: number = 0.5, imede: boolean = false, backClick: boolean = false): void {
		if (view.parent && view.parent.parent) {
			view.isClosed = false;
			view.parent.parent.addChild(view.parent);
			return;
		}
		view.touchEnabled = false;
		view.isClosed = false;
		let box = new Box(backClick);
		boxLayer.addChild(box);

		box.addView(view, fillAlpha, imede);

		showTopMask();
	}

	export function closeTopBox(imme: boolean = false): void {
		for (let i: number = boxLayer.numChildren - 1; i >= 0; i--) {
			let box: Box = boxLayer.getChildAt(i) as Box;
			if (box.isSame && !box.isBoxClosed()) {
				box.removeView(imme);
				return;
			}
		}
	}

	export function closeBox(view: BaseComponent, imme: boolean = false): void {
		for (let i: number = 0; i < boxLayer.numChildren; i++) {
			let box: Box = boxLayer.getChildAt(i) as Box;
			if (box.isSame && box.isSame(view) && !box.isBoxClosed()) {
				box.removeView(imme);
				return;
			}
		}
		console.error("没有此弹窗");
	}

	export function closeAllBoxes(imme: boolean = false): void {
		for (let i: number = boxLayer.numChildren - 1; i >= 0; i--) {
			let box: Box = boxLayer.getChildAt(i) as Box;
			if (box.isSame && !box.isBoxClosed()) {
				box.removeView(imme);
			}
		}
	}

	export let boxLayer: eui.UILayer;
	let tipLayer: eui.UILayer;
	export function initBox(uiRoot: eui.UILayer): void {
		boxLayer = new eui.UILayer();
		uiRoot.addChild(boxLayer);
		boxLayer.touchEnabled = false;

		tipLayer = new eui.UILayer();
		uiRoot.addChild(tipLayer);
		tipLayer.touchEnabled = false;
	}

	export function showTipView(view: eui.Component): void {
		tipLayer.addChild(view);
	}

	export function removeTipView(view: eui.Component): void {
		if (view.parent) {
			view.parent.removeChild(view);
		}
	}
}